<?php


namespace app\admin\service;


use app\admin\model\Applet;

use think\facade\Db;

class AppletService extends BaseService
{
    /**
     * 构造函数
     * @author 牧羊人
     * @since 2020/11/15
     * AdSortService constructor.
     */
    public function __construct()
    {
        $this->model = new Applet();
    }

    public function getUserInfo()
    {
        $info = Db::name('mini_match')->where('id', '=', 1)->find();


        if ($info) {
            $result['app_id'] = $info['app_id'];
            $result['wc_hid'] = $info['wc_hid'];
            $result['key'] = $info['key'];
            $result['api_key'] = $info['api_key'];
            $result['apiclient_cert'] = $info['apiclient_cert'];
            $result['apiclient_key'] = $info['apiclient_key'];
            $result['mch_id'] = $info['mch_id'];
            $result['sub_mch_id'] = $info['sub_mch_id'];
        } else {
            $result['app_id'] = '';
            $result['wc_hid'] = '';
            $result['key'] = '';
            $result['api_key'] = '';
        }
        return message("操作成功", true, $result);
    }

    public function updateUserInfo()
    {


        // 参数
        $param = request()->param();

        // 个人信息
        $data = [
            'app_id' => $param['app_id'],
            'key' => $param['key'],
            'wc_hid' => $param['wc_hid'],
            'api_key' => $param['api_key'],
            'mch_id' => $param['mch_id'],
            'sub_mch_id' => $param['sub_mch_id'],

        ];
        if (isset($param['apiclient_cert'])) {
            if($param['apiclient_cert']!=""){
                $filename = './uploads/key/' . 'apiclient_cert.pem';

                $fp = fopen($filename, "w");  //w是写入模式，文件不存在则创建文件写入。

                fwrite($fp, $param['apiclient_cert']);
                $data['apiclient_url'] = $filename;
                $data['apiclient_cert'] = $param['apiclient_cert'];

                $filename = './uploads/key/' . 'apiclient_key.pem';
                $fp = fopen($filename, "w");  //w是写入模式，文件不存在则创建文件写入。
                fwrite($fp, $param['apiclient_key']);
                $data['apiclient_key'] = $param['apiclient_key'];
                $data['apiclient_key_url'] = $filename;
            }

        }
        $info = Db::name('mini_match')->where('id', '=', 1)->find();
        $data['id'] = $info['id'];
        $result = $this->model->edit($data);
        if (!$result) {
            return message("更新资料信息失败", false);
        }
        return message("更新资料信息成功");
    }
}